import 'package:flutter/material.dart';
import 'package:xiecheng_lvyou/dao/travel_dao.dart';
import 'package:xiecheng_lvyou/dao/travel_tab_dao.dart';
import 'package:xiecheng_lvyou/model/travel_tab_model.dart';
import 'package:xiecheng_lvyou/pages/travel_tab_page.dart';

class TravelPage extends StatefulWidget {
  const TravelPage({super.key});
  @override
  State<TravelPage> createState() => _TravelPageState();
}

class _TravelPageState extends State<TravelPage>
    with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin {
  late TabController _controller;
  List<TravelTab> tabs = [];
  late TravelTabModel travelTabModel;

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    TravelDao.fetch('tourphoto_global1', 1, 1)
        .then((value) {})
        .catchError((onError) {
      print(onError.toString());
    });

    TravelTabDao.fetch().then((TravelTabModel model) {
      _controller = TabController(length: model.tabs?.length ?? 0, vsync: this);
      setState(() {
        if (model.tabs != null) {
          tabs = model.tabs!;
        }
        travelTabModel = model;
      });
    }).catchError((onError) {});
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      body: Column(
        children: [
          Container(
            // color: Colors.red,
            // height: 30,
            margin: const EdgeInsets.only(top: 30, left: 20, right: 20),
            child: tabs.isEmpty
                ? Container()
                : TabBar(
                    tabAlignment: TabAlignment.start,
                    padding: const EdgeInsets.all(0),
                    controller: _controller,
                    dividerHeight: 0,
                    isScrollable: true,
                    labelColor: Colors.black,
                    labelPadding: const EdgeInsets.fromLTRB(10, 0, 10, 5),
                    indicator: const UnderlineTabIndicator(
                      borderSide:
                          BorderSide(color: Color(0xff2fcfbb), width: 3),
                      insets: EdgeInsets.only(bottom: 10),
                    ),
                    tabs: tabs.map((TravelTab tab) {
                      return Tab(text: tab.labelName);
                    }).toList()),
          ),
          // Expanded(child: Container(color: Colors.red,)),
          // Expanded(child: Container(color: Colors.green,))
          Expanded(
              child: tabs.isEmpty
                  ? Container()
                  : TabBarView(
                      controller: _controller,
                      children: tabs.map((TravelTab tab) {
                        return TravelTabPage(
                            groupChannelCode: tab.groupChannelCode ?? '');
                      }).toList()))
        ],
      ),
    );
  }
}
